package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-30
 */
@RestController
@RequestMapping("/eduservice/chapter")
@Slf4j
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    //获取所有章节列表，根据课程id进行查询里面的章节和小节
    @GetMapping("/getChapterVideo/{courseId}")
    private Result getChapterVideo(@PathVariable(value = "courseId") String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return Result.ok().data("allChapterVideo",list);
    }

    //添加章节
    @PostMapping("/addChapter")
    public Result addChapter(@RequestBody EduChapter eduChapter) {
        chapterService.save(eduChapter);
        return Result.ok();
    }

    //根据章节查询id
    @GetMapping("/getChapterInfo/{chapterId}")
    public Result getChapterInfo(@PathVariable String chapterId) {
        EduChapter chapter = chapterService.getById(chapterId);
        return Result.ok().data("chapter",chapter);
    }
    //修改章节
    @PostMapping("/updateChapter")
    public Result updateChapter(@RequestBody EduChapter eduChapter) {
        chapterService.updateById(eduChapter);
        return Result.ok();
    }
    //删除章节
    @DeleteMapping("/{chapterId}")
    public Result deleteChapter(@PathVariable String chapterId) {
       boolean flag = chapterService.deleteChapter(chapterId);
       log.info("删除章节数据: => {} 是否删除: => {}",chapterId,flag);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
}







